from flask import Blueprint,request,jsonify,make_response
from  flask import  current_app as app
import json
import os
import uuid
import  datetime
from utils.mylist import myfile
from utils.db import  db
from utils.comm import xtree
from utils.myes import Es
from utils.comm import  get_data
from utils.jwts import myjwt
from utils.myacl import acl
from utils.myrbac import rbac
from utils.redisconn import  r

bp_message = Blueprint('message',__name__)


#展示消息 然后添加消息
@bp_message.route('/getmessage/',methods=['POST','GET'])
async def getmessage():
    if request.method =='POST':
        #接收参数写入数据库
        data = get_data()
        print(eval(data['idlst']),type(eval(data['idlst'])))
        #添加到消息通知表中
        sql = "insert into message_list values(0,'%s','%s','%s',1,%d,%d,%d,'%s') "%(data['title'],data['content'],data['stime'],int(data['type']),int(data['priority']),int(data['messagetype']),data['etime'])
        # print(sql)
        db.insert(sql)

        #查询新添加的
        sql1 = "select * from message_list where stime='%s' and status=%d"%(data['stime'],int(data['type']))
        print(sql1)
        res1 = db.find_one(sql1)
        #如果有传过来的用户列表 在用户列表页添加
        if int(res1['type']) == 1 and res1['stime'] == data['stime']:
            #默认未读
            for i in eval(data['idlst']):
                sql2 = "insert into user_message_list values(0,%d,%d,2)"%(int(res1['id']),int(i))
                db.insert(sql2)

        return jsonify({'code':200})
        
    else:
        userid = request.args.get('userid')
        
        if int(userid) == 4:
            #展示所有消息
            sql = "select * from message_list"
            res = db.find_all(sql)
            return jsonify({"code":200,'list':res})
        else:
            #展示用户消息   链表查询展示信息
            sql = "select ml.title,ml.content,ml.messagetype,ml.priority,ml.type,ml.status,ml.stime from message_list as ml left join user_message_list as uml on ml.id=uml.messageid where uml.userid=%d "%(int(userid))
            res = db.find_all(sql)
            return jsonify({"code":200,'list':res})




#用户积分兑换系统

#登录
@bp_message.route('/jlogin/',methods=['POST',"GET"])
def jlogin():
    if request.method == "POST":
        data = get_data()
        sql = "select * from jusers where name='%s'"%(data['name'])
        res = db.find_one(sql)
        print(res)
        if res:
            if res['name'] == data['name']:
                #查询权限
                sql1 = 'select j.name,j.url from jusers as ju left join juser_resource as jr on ju.id=jr.juserid left join jresource as j on jr.jresourceid=j.id where ju.id=%d'%(int(res['id']))
                res1 = db.find_all(sql1)
                lst = []
                for i in res1:
                    lst.append(i['url'])
                print(lst)
                return jsonify({'code':200,'mes':'登录成功','userid':res['id'],'name':res['name'],'reslist':lst})
            else:
                return jsonify({'code':10010,'mes':'密码或用户名有误'})
        else:
            return jsonify({"code":20020,'mes':'无该用户'})
    else:
        pass


#管理员审核
@bp_message.route('/jshenhe/',methods=['POST',"GET"])
def jshenhe():
    if request.method == "POST":
        pass
    else:
        #查询的id
        id = request.args.get('id')
        xsid = request.args.get('xsid')
        status = request.args.get('status')
        print(id,xsid,status)
        sql = "select * from xiaoshuo where id=%d"%(int(xsid))
        res = db.find_one(sql)
        if not res:
            return jsonify({'code':10022,'mes':'无该小说'})
        
        #加积分
        if int(status) == 2:
            #修改状态
            sql1 = "update xiaoshuo set status=%d where id=%d"%(int(status),int(xsid))
            db.update(sql1)
            #加积分
            sql2 = "select * from jusers where id=%d"%(int(id))
            res2 = db.find_one(sql2)
            count = int(res2['jifen'])+2
            sql3 = "update jusers set jifen=%d where id=%d"%(int(count),int(id))
            db.update(sql3)
        elif int(status) == 3:
            
            #修改状态
            sql1 = "update xiaoshuo set status=%d where id=%d"%(int(status),int(xsid))
            db.update(sql1)
            #不通过写入记录表
            sql = "insert into xiaoshuo_record values(0,'%s',%d) "%(res['name'],3)
            db.insert(sql)
        return jsonify({'code':200})


# #echars 实时监控积分的增长
@bp_message.route('/jechars/',methods=['POST',"GET"])
def jechars():
    if request.method == "POST":
        pass
    else:
        #获取
        sql = "select * from jusers"
        res = db.find_all(sql)
        lst = []
        for i in res:
            dic = {'name':i['name'],'value':i['jifen']}
            lst.append(dic)
        # print(lst)
        return jsonify({'code':200,'list':lst})










